package com.gxa.service.impl;

import com.gxa.common.JsonResult;
import com.gxa.common.R;
import com.gxa.mapper.AfterSalesMapper;
import com.gxa.pojo.AS_Contact_Customer;
import com.gxa.pojo.After_Sales;
import com.gxa.pojo.Employee;
import com.gxa.pojo.Goods;
import com.gxa.service.AfterSalesService;
import com.gxa.utils.MapUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.*;

@Service
public class AfterSalesServiceImpl implements AfterSalesService{
    @Autowired(required = false)
    private AfterSalesMapper afterSalesMapper;
    @Override
    public JsonResult queryList(Map<String,String> map) {
        Integer param01 = (Integer.parseInt(map.get("CurrentPage"))-1)*Integer.parseInt(map.get("PageSize"));
        Integer param02 = Integer.parseInt(map.get("CurrentPage"))*Integer.parseInt(map.get("PageSize"));
        String str = map.get("customer_id");
        map.keySet().removeIf(key -> key.equals("customer_id"));
        map.keySet().removeIf(key -> key.equals("CurrentPage"));
        map.keySet().removeIf(key -> key.equals("PageSize"));
        map.put("a.customer_id",str);
        System.out.println(map);
        List<After_Sales> list = afterSalesMapper.queryList(param01,param02,MapUtils.delNullMap(map));
        Integer count = afterSalesMapper.count(MapUtils.delNullMap(map));
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode(200);
        jsonResult.setMsg("查询成功");
        jsonResult.setCount(count);
        jsonResult.setData(list);
        return jsonResult;
    }

    @Override
    public JsonResult specificQueryList(Map<String, String> map) {
        Integer param01 = (Integer.parseInt(map.get("CurrentPage"))-1)*Integer.parseInt(map.get("PageSize"));
        Integer param02 = Integer.parseInt(map.get("CurrentPage"))*Integer.parseInt(map.get("PageSize"));
        String str = map.get("str");
        List<After_Sales> list = afterSalesMapper.specificQueryList(param01,param02,str);
        Integer count = afterSalesMapper.specificCount(str);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode(200);
        jsonResult.setMsg("查询成功");
        jsonResult.setCount(count);
        jsonResult.setData(list);
        return jsonResult;
    }

    @Override
    public R addForm(After_Sales after_sales) {
        afterSalesMapper.addForm(after_sales);
        R r = new R();
        r.setCode(200);
        r.setMsg("新增成功");
        return r;
    }

    @Override
    public R upDate(AS_Contact_Customer after_sales) {
        R r = new R();
        try {
            Integer id = after_sales.getA_s_id();
            Map map = MapUtils.delNullMap(MapUtils.objectToMap(after_sales));
            map.keySet().removeIf(key -> key.equals("a_s_id"));
            afterSalesMapper.update(map,id);
            r.setCode(200);
            r.setMsg("操作成功");
        } catch (IllegalAccessException e) {
            r.setCode(500);
            r.setMsg("系统错误，请联系管理人员 ");
            e.printStackTrace();
        }
        return r;
    }

    @Override
    public R del(Integer a_s_id) {
        R r = new R();
        afterSalesMapper.del(a_s_id);
        r.setCode(200);
        r.setMsg("操作成功");
        return r;
    }

    @Override
    public R queryEmp() {
        R r = new R();
        List<Employee> list = afterSalesMapper.queryEmp();
        r.setCode(200);
        r.setMsg("查询成功");
        r.setData(list);
        return r;
    }

    @Override
    public R queryGoods(Integer customer_id) {
        R r = new R();
        List<Goods>list = afterSalesMapper.queryGoods(customer_id);
        r.setCode(200);
        r.setMsg("查询成功");
        r.setData(list);
        return r;
    }

    @Override
    public R countUnderEmp() {
        R r = new R();
        List<Map>list=afterSalesMapper.countUnderEmp();
        System.out.println(list);
        r.setCode(200);
        r.setMsg("查询成功");
        r.setData(list);
        return r;
    }

    @Override
    public R countUnderDept() {
        R r = new R();
        List<Map>list=afterSalesMapper.countUnderDept();
        System.out.println(list);
        r.setCode(200);
        r.setMsg("查询成功");
        r.setData(list);
        return r;
    }

    @Override
    public R delCustomerOrders(Integer customer_id) {
        R r = new R();
        afterSalesMapper.delCustomerOrders(customer_id);
        r.setCode(200);
        r.setMsg("删除成功");
        return r;
    }
}
